//    Copyright (C) Kherty.  All rights reserved.
using OpenLS.Core.Serialization;
using OpenLS.Spreadsheet.Internals;

namespace OpenLS.Spreadsheet
{
    class RichTextRunProperties : IOfficeXmlSerializable
    {

        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read("rPr"))
            {
                while (c.ReaderLocalName != null)
                {
                    switch (c.ReaderLocalName)
                    {
                        case "sz":
                            Size = c.ReadOptionalElementDoubleAttribute("sz", "val", 11.0); break;
                        case "b":
                            if (c.ReadOptionalElementBooleanAttribute("b", false))
                                Bold = true;
                            break;
                        case "color":
                            Foreground = SpreadsheetColor.ReadColor("color", c);
                            break;
                        case "rFont":
                            Name = c.ReadOptionalElementStringAttribute("rFont", "val");
                            break;
                        case "family": Family = c.ReadOptionalElementIntegerAttribute("family", "val", 0);
                            break;

                        case "condense":
                            Condense = c.ReadOptionalElementBooleanAttribute("condense", false);
                            break;
                        case "extend":
                            Extend = c.ReadOptionalElementBooleanAttribute("extend", false);
                            break;
                        case "u":
                            Underline = c.ReadOptionalElementEnumAttribute<UnderlineType>("u", "val", UnderlineType.None, UnderlineType.None);
                            break;
                        case "vertAlign":
                            VerticalAlignment = c.ReadElementEnumAttribute<VerticalRunAlignment>("vertAlign");
                            break;
                        case "scheme":
                            FontScheme = c.ReadElementEnumAttribute<FontSchemeType>("scheme");
                            break;
                        case "charset":
                            CharSet = c.ReadOptionalElementIntegerAttribute("charset"); break;
                        case "i":
                            Italic = c.ReadOptionalElementBooleanAttribute("i", false);
                            break;
                        case "strike":
                            Strike = c.ReadOptionalElementBooleanAttribute("strike", false);
                            break;
                        case "outline":
                            Outline = c.ReadOptionalElementBooleanAttribute("outline", false);
                            break;
                        case "shadow":
                            Shadow = c.ReadOptionalElementBooleanAttribute("shadow", false);
                            break;
                    }
                }
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("rPr"))
            {
                c.WriteOptionalElementDoubleAttribute("sz", "val", Size, 11.0);
                if (Bold) //\\ != FontWeights.Normal)
                    c.WriteOptionalElementBooleanAttribute("b", true, false);
                if (Foreground != null)
                    Foreground.WriteXmlAs("color", c);
                c.WriteOptionalElementStringAttribute("rFont", Name);
                c.WriteOptionalElementIntegerAttribute("family", Family, 0);
                c.WriteOptionalElementBooleanAttribute("condense", Condense, false);
                c.WriteOptionalElementBooleanAttribute("extend", Extend, false);
                c.WriteOptionalElementEnumAttribute("u", Underline, UnderlineType.None, UnderlineType.None);
                c.WriteOptionalElementEnumAttribute("vertAlign", VerticalAlignment, VerticalRunAlignment.Baseline, VerticalRunAlignment.Baseline);
                c.WriteOptionalElementEnumAttribute("scheme", FontScheme, FontSchemeType.None, FontSchemeType.None);
                c.WriteOptionalElementIntegerAttribute("charset", CharSet);
                c.WriteOptionalElementBooleanAttribute("i", Italic, false);
                c.WriteOptionalElementBooleanAttribute("strike", Strike, false);
                c.WriteOptionalElementBooleanAttribute("outline", outline, false);
                c.WriteOptionalElementBooleanAttribute("shadow", Shadow, false);
            }
        }

        #endregion

        private int? charset;

        public int? CharSet
        {
            get { return charset; }
            set { charset = value; }
        }
	

        private bool italic;
            
        public bool Italic
        {
            get { return italic; }
            set { italic = value; }
        }
	

        private bool strike;

        public bool Strike
        {
            get { return strike; }
            set { strike = value; }
        }
	

        private bool outline;

        public bool Outline
        {
            get { return outline; }
            set { outline = value; }
        }
	

        private bool shadow;

        public bool Shadow
        {
            get { return shadow; }
            set { shadow = value; }
        }
	

        private bool condense;

        public bool Condense
        {
            get { return condense; }
            set { condense = value; }
        }
	

        private bool extend;

        public bool Extend
        {
            get { return extend; }
            set { extend = value; }
        }
	

        private UnderlineType underline;

        public UnderlineType Underline
        {
            get { return underline; }
            set { underline = value; }
        }
	

        private VerticalRunAlignment verticalAlignment;

        public VerticalRunAlignment VerticalAlignment
        {
            get { return verticalAlignment; }
            set { verticalAlignment = value; }
        }
	

        private FontSchemeType fontScheme;

        public FontSchemeType FontScheme
        {
            get { return fontScheme; }
            set { fontScheme = value; }
        }
	


        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private double size = 11;

        public double Size
        {
            get { return size; }
            set { size = value; }
        }
        private SpreadsheetColor foreground;

        public SpreadsheetColor Foreground
        {
            get { return foreground; }
            set { foreground = value; }
        }
        private int family;

        public int Family
        {
            get { return family; }
            set { family = value; }
        }
	

        private bool _b;

        public bool Bold
        {
            get { return _b; }
            set { _b = value; }
        }




        internal RichTextRunProperties Clone()
        {
            return MemberwiseClone () as RichTextRunProperties;
        }

        protected bool Equals(RichTextRunProperties richTextRunProperties)
        {
            if (richTextRunProperties == null) return false;
            if (!Equals(charset, richTextRunProperties.charset)) return false;
            if (!Equals(italic, richTextRunProperties.italic)) return false;
            if (!Equals(strike, richTextRunProperties.strike)) return false;
            if (!Equals(outline, richTextRunProperties.outline)) return false;
            if (!Equals(shadow, richTextRunProperties.shadow)) return false;
            if (!Equals(condense, richTextRunProperties.condense)) return false;
            if (!Equals(extend, richTextRunProperties.extend)) return false;
            if (!Equals(underline, richTextRunProperties.underline)) return false;
            if (!Equals(verticalAlignment, richTextRunProperties.verticalAlignment)) return false;
            if (!Equals(fontScheme, richTextRunProperties.fontScheme)) return false;
            if (!Equals(name, richTextRunProperties.name)) return false;
            if (size != richTextRunProperties.size) return false;
            if (!Equals(foreground, richTextRunProperties.foreground)) return false;
            if (family != richTextRunProperties.family) return false;
            if (!Equals(_b, richTextRunProperties._b)) return false;
            return true;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(this, obj)) return true;
            return Equals(obj as RichTextRunProperties);
        }

        public override int GetHashCode()
        {
            int result = charset.GetHashCode();
            result = 29*result + italic.GetHashCode();
            result = 29*result + strike.GetHashCode();
            result = 29*result + outline.GetHashCode();
            result = 29*result + shadow.GetHashCode();
            result = 29*result + condense.GetHashCode();
            result = 29*result + extend.GetHashCode();
            result = 29*result + underline.GetHashCode();
            result = 29*result + verticalAlignment.GetHashCode();
            result = 29*result + fontScheme.GetHashCode();
            result = 29*result + (name != null ? name.GetHashCode() : 0);
            result = 29*result + size.GetHashCode();
            result = 29*result + (foreground != null ? foreground.GetHashCode() : 0);
            result = 29*result + family;
            result = 29*result + _b.GetHashCode();
            return result;
        }
    }
}
